Security News
Highlights from the 2024 Rails Community Survey
A record 2,709 developers participated in the 2024 Ruby on Rails Community Survey, revealing key tools, practices, and trends shaping the Rails ecosystem.
jsonp-retry
Advanced tools
A simple implementation of jsonp, it provides timeout and retry function, when the main request is failed because of timeout, there will be many retries.
And the surprise was, you can store the response data to localStorage with jsonp-retry automatically, and then it can get the data from localStorage without request from network if it detects the store data is available.Of course, you can custom the store check rule.You will learn how to use this function from the below.
Happy to use!😘🤡
If you use webpack or browserify, you can install this package with npm or yarn
npm install jsonp-retry --save
or
yarn add jsonp-retry
Then require and use it
import jsonp from 'jsonp-retry'
jsonp('//example.com/xxx', {
timeout: 3000
}, function (err, data) {
if (err) {
throw err
}
// Todo with data
})
or use in Promise way
import jsonp from 'jsonp-retry'
jsonp('//example.com/xxx', {
timeout: 3000
}).then(data => {
// Todo with data
}).catch(err => {
throw err
})
jsonp-retry provides a flexible way of using it, usually you can pass an callback to process the request result, and more elegantly, if your environment support Promise, you can use jsonp-retry in Promise way.
Usually it accept 3 arguments, url
、opts
、cb
, of course cb
is not required.It will return an instance of Promise if your environment support.
url
(String
): the url to fetch dataopts
(Object
): optional, config the jsonp method
jsonp
(String
): appoint the jsonp request url key (defaults to callback
)name
(String
): appoint the jsonp callback function (defaults to a string with prefix '__jsonp' and current time stamp like __jsonp1504108585883
)params
(String
or Object
): appoint the jsonp request url params, it can be a query string like a=1&b=2&c=3
, or it can be an object like { a: 1, b: 2 }
,it will transform to a query stringcache
(Boolean
): appoint the jsonp request url whether to be cached (defaults to false
)timeout
(Number
): how long after a timeout error is emitted (defaults to 2000ms
)retryTimes
(Number
): when the request is timeout it can appoint how many times to retry (defaults to 2
)backup
(String
or Array
): appoint a backup url or multiple backup urls, if the url param request is failed, it will use backup url(s) to get backup data for correct displaydataCheck
(Function
): a function to check the response data is correct, the function will accept the response data as arguments, if it returns false jsonp-retry would use the backup url(s) to fetch data if there has backup or it will emit an erroruseStore
(Boolean
): appoint to use localStorage to get or set data or not (defaults to false
)storeCheckKey
(String
): appoint the key to check in store, it is generally a field of the data which get from localStorage, with it we can get something like version of the datastoreSign
(String
): the new data version, you can compare it with the old version of the data in localStoragestoreCheck
(Function
): check the data from localStorage if correct (storeCheck
has been defined inside, you can pass your own function to cover)charset
(String
): appoint the charset of the script tag (defaults UTF-8
)cb
(function
): the request callback, accept 2 arguments, err
tells if there has an error, data
is the response dataIn the same way, you can put the first url
argument into opts
, so there will only need to pass only 2 arguments like
opts
(Object
): optional, config the jsonp method
url
(String
): the url to fetch datacb
(function
): the request callback, accept 2 arguments, err
tells if there has an error, data
is the response dataMIT License
Copyright (c) 2017 Li,Weitao
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
FAQs
A simple jsonp with retry and storage
The npm package jsonp-retry receives a total of 2,605 weekly downloads. As such, jsonp-retry popularity was classified as popular.
We found that jsonp-retry demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
A record 2,709 developers participated in the 2024 Ruby on Rails Community Survey, revealing key tools, practices, and trends shaping the Rails ecosystem.
Security News
In 2023, data breaches surged 78% from zero-day and supply chain attacks, but developers are still buried under alerts that are unable to prevent these threats.
Security News
Solo open source maintainers face burnout and security challenges, with 60% unpaid and 60% considering quitting.